<?php

namespace app\manage\controller;

use support\Request;
use app\manage\controller\Base;
use Gregwar\Captcha\CaptchaBuilder;
use think\facade\Db;

class Log extends Base
{
    public function index(Request $request)
    {
        return view('log/index', [

        ]);
    }


    public function api(Request $request){
        $data=$request->all();
        if($data['type']=='index'){
            if(!isset($data['end'])){
                $data['end']=0;
            }
            if(!isset($data['start'])){
                $data['start']=0;
            }
            if($data['end'] or $data['start']){
                if($data['end'] and $data['start']){
                    $so=[
                        ['create_time','>',$data['start']],
                        ['create_time','<',$data['end']],
                    ];
                }
                if(!$data['end'] and $data['start']){
                    $so=[
                        ['create_time','>',$data['start']],
                    ];
                }
                if($data['end'] and !$data['start']){
                    $so=[
                        ['create_time','<',$data['end']],
                    ];
                }
            }else{
                $so=true;

            }
            $count=Db::name('log')->where($so)->count();
            $info=Db::name('log')->where($so)->page($data['page'],$data['limit'])->order('create_time desc')->select();

            return json([
                'code'=>0,
                'msg'=>'ok',
                'count'=>$count,
                'data'=>$info
            ]);
        }


        //批量删除
        if($data['type']=='del_all'){
            $arr=$data['data'];
            if(in_array(1,$arr)){
                return json([
                    'code'=>1,
                    'msg'=>'不能删除总管理员！',
                ]);
            }
            Db::name('admin')->where('id','in',$arr)->delete();
            return json([
                'code'=>0,
                'msg'=>'删除成功',
            ]);
        }
        //删除一个
        if($data['type']=='del_one'){
            $one=$data['data'];
            if($one==1){
                return json([
                    'code'=>1,
                    'msg'=>'不能删除总管理员！',
                ]);
            }
            Db::name('admin')->where('id',$one)->delete();
            return json([
                'code'=>0,
                'msg'=>'删除成功',
            ]);
        }

        return json([
            'code'=>2,
            'msg'=>'调用失败'
        ]);

    }
    
}
